<?php
/**
 * 此类用于插件访问和操作特定数据
 * 
 * @package Plugin
 * @category Plugin
 * 
 * @author leo
 * @version v1.0
 * @copyright xer345.com 2013-04-23
 * @link http://xer345.com
 * 
 * @todo 实现以下类型数据的增、删、改、查询等操作，包括：商品、新闻、后台菜单
 */
class iData {
	
	/**
	 * 实例句柄数组
	 */
	public static $instances = array();

	/**
	 * 获取实例
	 * 
	 * @return mixed 实例句柄
	 * 
	 * @param  string $clsname 类名
	 */
	public static function get_instance($clsname){
		if(isset(self::$instances[$clsname])){
			return slef::$instances[$clsname];
		}
		return new $clsname();
	}

	/*******************************************************************************
	 * 新闻，仅最新动态分类，可插入、读取列表与详情
	 *******************************************************************************/

	/**
	 * 添加新闻
	 * <pre>用法示例：
	 * 	$newsid = add_news(array(
	 * 		'catid'		=> 分类ID,
	 * 		'title'		=> 标题,
	 * 		'url'		=> 链接,
	 * 		'summary'	=> 摘要,
	 * 		'content'	=> 内容,
	 * 	);
	 * 结果：136</pre>
	 * 
	 * @example r:\cls\DBase.class.php
	 * @return int >0 新闻ID; <0 错误类型
	 * 
	 * @param array $data 新闻数据数组
	 */
	public static function add_news($data){
		$_oNews = & self::get_instance('News');
		return $_oNews->edit($data);
	}

	/**
	 * 取单条新闻
	 * 
	 * @return array 新闻数据数组
	 * 
	 * @param  int $newsid 新闻ID
	 */
	public static function get_news($newsid){
		$_oNews = & self::get_instance('News');
		return $_oNews->get_row($newsid, 'id,catid,title,style,url,summary,content,publish,rank,hits,addtime,updatetime');
	}
 
	/**
	 * 取新闻列表
	 * 
	 * @return array 新闻列表数据数组
	 *
	 * @param integer $catid 分类ID
	 * @param  string  $title 标题关键词
	 * @param  integer $page 页码
	 * @param  integer $pagesize 返回数量
	 */
	public static function news_list($catid = '', $keyword = '', $page = 1, $pagesize = 10){
		$select = array('catid' => $catid, 'keyword' => $keyword, 'isdel' => 0);
		$pagesize = min(100, max(1, intval($pagesize)));	// 列表单次限制最多1百条
		$_oNews = & self::get_instance('News');
		$data = $_oNews->get_list($fields = '`id`,`catid`,`title`,`style`,`url`,`summary`,`content`,`publish`,`rank`,`hits`,`addtime`,`updatetime`', $select, $order = array('rank' => 'DESC', 'id' => 'DESC'), $page, $pagesize, 0);
		if(empty($data['list'])){
			return array();
		}
		return $data['list'];
	}

	/*******************************************************************************
	 * 商品，读取
	 *******************************************************************************/

	public static function get_goods($goodsid){
		$_oGoods = & self::get_instance('Goods');
		return $_oGoods->get_row($goodsid, $fields = '`goodsid`,`sn`,`name` as `goodsname`,`catid`,`brand`,`weight`,`weightunit`,`price`,`cover`,`summary`,`describe`,`views`,`sales`,`comments`', $full = true, $cache = true);
	}

	/**
	 * 取商品列表
	 * 
	 * @return array            商品列表数据数组
	 * 
	 * @param  integer  $catid    分类ID
	 * @param  string  $keyword  商品名称关键词
	 * @param  string  $orderby  排序，可用排序：id,price,views,sales,comments
	 * @param  integer $page     页码
	 * @param  integer $pagesize 返回数量
	 */
	public static function goods_list($catid = '', $keyword = '', $orderby = '', $page = 1, $pagesize = 10){
		$select = array('catid' => $catid, 'keyword' => $keyword, 'isdel' => 0);
		if($orderby != ''){
			switch($orderby){
				case 'id':
					$orderby = array('orderby_id' => 'DESC');
					break;
				case 'price':
					$orderby = array('orderby_price' => 'DESC');
					break;
				case 'views':
					$orderby = array('orderby_views' => 'DESC');
					break;
				case 'sales':
					$orderby = array('orderby_sales' => 'DESC');
					break;
				case 'comments':
					$orderby = array('orderby_comments' => 'DESC');
					break;
			}
		}
		$pagesize = min(100, max(1, intval($pagesize)));	// 列表单次限制最多1百条
		$_oGoods = & self::get_instance('Goods');
		$data = $_oGoods->get_list($fields = '`goodsid`,`sn`,`name` as `goodsname`,`catid`,`brand`,`weight`,`weightunit`,`price`,`cover`,`summary`,`describe`,`views`,`sales`,`comments`', $select, $orderby, $page, $pagesize, 0);
		if(empty($data['list'])){
			return array();
		}
		return $data['list'];
	}
}
?>
